/**
 * Nombre : PermisoPerfilAction.java 
 * Versi�n : 1.1 
 * Creaci�n : 14/11/2008 
 * Autor : Orlando Pihue Montoya 
 * Usuario : P020912 
 * Finalidad : Consulta de Perfiles de Usuario
 * Modificacion: << fecha de modificaci�n>> 
 * Autor : <<quien actualizo el archivo>> 
 * Usuario : <<c�digo de usuario modificador>> 
 * Detalle : <<descripci�n de la modificaci�n>>
 */
 
package igrupobbva.pe.sistemareniec.actions;

import igrupobbva.pe.sistemareniec.forms.PermisoPerfilForm;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.grupobbva.iici.serializable.PermisoPerfil;
import com.grupobbva.iici.service.PerfilService;
import com.grupobbva.iici.util.ConstantesUtil;
import com.grupobbva.iici.util.SpringUtils;


public class PermisoPerfilAction extends DispatchAction

{
	static Logger logger = Logger.getLogger(PermisoPerfilAction.class.getName());
	
	PerfilService perfilService;
	 
	public ActionForward inicio(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		
		String flag1=(String)sesion.getAttribute("root");
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
	
		ActionForward forward = new ActionForward(); // return value
		perfilService=SpringUtils.getBean(request, PerfilService.class);
		PermisoPerfilForm objForm = (PermisoPerfilForm) form;
		try {			
			List listPermisos = perfilService.obtenerTodosPerfilPermiso(flag);
			request.getSession().setAttribute("listaPermisoPerfil", listPermisos);
			objForm.setCheckedA("checked");
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error");
		} else {
			forward = mapping.findForward("ok");
		}
		return (forward);
	}
	
	public ActionForward mostrarNuevo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
		ActionForward forward = new ActionForward(); // return value
		PermisoPerfilForm permisoForm = (PermisoPerfilForm) form;
		perfilService=SpringUtils.getBean(request, PerfilService.class);
		try { 
			List listPerfil = perfilService.obtenerTodosPerfiles(flag);
			permisoForm.setCodPerfil("");
			permisoForm.setTipoAcceso("");
			permisoForm.setValorRCP("");
			request.setAttribute("listPerfil", listPerfil);	
			request.setAttribute("method", "nuevo");
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("nuevo"); 
		}
		return (forward);
	}
	
	public ActionForward nuevo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward();
		PermisoPerfilForm permisoForm = (PermisoPerfilForm) form;
		perfilService=SpringUtils.getBean(request, PerfilService.class);
		Vector listMenu = null;
		String codPerfil = (String) request.getParameter("codPerfil");
		String tipoAcceso = (String) request.getParameter("tipoAcceso");
		String valorRCP = (String) request.getParameter("valorRCP");
		int flag =Integer.parseInt((String) sesion.getAttribute("root")) ;
		
		ArrayList collecUsuario=new ArrayList();
		ArrayList listaTitulo = new ArrayList();
		
		try { 			
			/**OJO CHEKEAR**/
			PermisoPerfil permisoPerfil=new PermisoPerfil();
			permisoPerfil.setCodPerfil(codPerfil);
			permisoPerfil.setTipoAcceso(Integer.parseInt(tipoAcceso));
			permisoPerfil.setValorRCP(valorRCP);
			perfilService.registrar(permisoPerfil);
			request.getSession().setAttribute("listaPermisoPerfil",
					perfilService.obtenerTodosPerfilPermiso(flag));
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("ok"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String) sesion.getAttribute("root")) ;
		
		ActionForward forward = new ActionForward();
		PermisoPerfilForm perfilForm = (PermisoPerfilForm) form;
		perfilService=SpringUtils.getBean(request, PerfilService.class);
		Vector listMenu = null;
		String codPerfil = (String) request.getParameter("codPerfil");
		String tipoAcceso = (String) request.getParameter("tipoAcceso");
		String valorRCP = (String) request.getParameter("valorRCP");
		try {			
			String tempC[]=codPerfil.split(ConstantesUtil.SEPARADOR_CODIGOS);
			//String tempT[]=tipoAcceso.split(ConstantesUtil.SEPARADOR_CODIGOS);
			//String tempV[]=valorRCP.split(ConstantesUtil.SEPARADOR_CODIGOS);
			for(int i=0;i<tempC.length;i++){			
			String tempPihue[]=tempC[i].split(ConstantesUtil.SEPARADOR_ARROBA);		
				perfilService.eliminarPermisoPerfil(tempPihue[0], new Integer(tempPihue[1]), tempPihue[2]);
			}			
			request.getSession().setAttribute("listaPermisoPerfil", perfilService.obtenerTodosPerfilPermiso(flag));			
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("ok"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		return (forward);
	}
	
	public ActionForward mostrarEditar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
		ActionForward forward = new ActionForward(); 
		PermisoPerfilForm perfilForm = (PermisoPerfilForm) form;
		perfilService=SpringUtils.getBean(request, PerfilService.class);
		Vector listMenu = null;	
		String codPerfil = (String) request.getParameter("codPerfil");	
		String tipoAcceso = (String) request.getParameter("tipoAcceso");
		String valorRCP = (String) request.getParameter("valorRCP");	
		try { 
			request.getSession().setAttribute("listaPermisoPerfil",	perfilService.obtenerTodosPerfilPermiso(flag));
			request.setAttribute("listPerfil", perfilService.obtenerTodosPerfiles(flag));
			PermisoPerfil perfil = perfilService.obtenerPermisoPerfilCodigo(codPerfil,new Integer(tipoAcceso),valorRCP);
			perfilForm.setCodPerfil(perfil.getCodPerfil());
			perfilForm.setDesPerfil(perfil.getDesPerfil());	
			perfilForm.setValorRCP(perfil.getValorRCP());
			
			request.setAttribute("cp",perfil.getCodPerfil());
			request.setAttribute("ta",perfil.getTipoAcceso());
		    request.setAttribute("vr",perfil.getValorRCP());
		    request.setAttribute("method", "editar");
						
		} catch (Exception e) {
			e.printStackTrace();
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("nuevo"); 
		}
		return (forward);	
	}	
	
	public ActionForward editar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
		ActionForward forward = new ActionForward();
		PermisoPerfilForm permisoForm = (PermisoPerfilForm) form;
		perfilService=SpringUtils.getBean(request, PerfilService.class);
		Vector listMenu = null;
		String codPerfil = (String) request.getParameter("codPerfil");
		String tipoAcceso = (String) request.getParameter("tipoAcceso");
		String valorRCP = (String) request.getParameter("valorRCP");	
		String cp = (String) request.getParameter("cp");
		String ta = (String) request.getParameter("ta");
		String vr = (String) request.getParameter("vr");
		
		ArrayList collecUsuario=new ArrayList();
		ArrayList listaTitulo = new ArrayList();
		
		try { 		
			PermisoPerfil permisoPerfil1=new PermisoPerfil();
			permisoPerfil1.setCodPerfil(codPerfil);
			permisoPerfil1.setTipoAcceso(Integer.parseInt(tipoAcceso));
			permisoPerfil1.setValorRCP(valorRCP.toUpperCase());
			
			PermisoPerfil permisoPerfil2=new PermisoPerfil();
			permisoPerfil2.setCodPerfil(cp);
			permisoPerfil2.setTipoAcceso(Integer.parseInt(ta));
			permisoPerfil2.setValorRCP(vr.toUpperCase());
			
			perfilService.modificar(permisoPerfil1,permisoPerfil2);
			request.getSession().setAttribute("listaPermisoPerfil",perfilService.obtenerTodosPerfilPermiso(flag));
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("ok"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);
	}
}
